#!/bin/bash

WORKSPACE=$(pwd)

## 新建build目录
mkdir -p ${WORKSPACE}/build
cd ${WORKSPACE}/build

## 编译rapidjson
git clone https://github.com/miloyip/rapidjson.git
cd rapidjson
mkdir -p build
cd build
cmake ..
make && make install

## 编译hiredis
cd ${WORKSPACE}/build
wget https://github.com/redis/hiredis/archive/refs/tags/v0.14.0.tar.gz
tar -zxf v0.14.0.tar.gz
cd hiredis-0.14.0
make && make install

## 编译log4cplus
cd ${WORKSPACE}/build
wget https://github.com/log4cplus/log4cplus/releases/download/REL_2_0_8/log4cplus-2.0.8.tar.gz
tar -zxf log4cplus-2.0.8.tar.gz
cd log4cplus-2.0.8
chmod 755 configure
./configure
make && make install

## 编译uuid
cd ${WORKSPACE}/build
wget -O libuuid-1.0.3.tar.gz https://sourceforge.net/projects/libuuid/files/libuuid-1.0.3.tar.gz/download
tar -zxf libuuid-1.0.3.tar.gz
cd libuuid-1.0.3
chmod 755 configure
./configure
make && make install

## 编译oatpp
cd ${WORKSPACE}/build
wget https://github.com/oatpp/oatpp/archive/refs/tags/1.3.0.tar.gz
tar -zxf 1.3.0.tar.gz
cd oatpp-1.3.0
mkdir -p build
cd build
cmake ..
make && make install
cp /usr/local/lib/oatpp-1.3.0/liboatpp.a /usr/local/lib/

## 拷贝安全函数库
cp ${WORKSPACE}/third_party/HwSecureC/libs/libsecurec.so /usr/local/lib

## 编译产物
cd ${WORKSPACE}/build
cmake ..
make

## 拷贝产物和配置文件
mkdir -p /opt/master/conf
cp ${WORKSPACE}/build/masterServer /opt/master/
cp ${WORKSPACE}/master.conf /opt/master/conf/
cp ${WORKSPACE}/version.conf /opt/master/conf/
cp ${WORKSPACE}/log.properties /opt/master/conf/

## 拷贝检测脚本
cp ${WORKSPACE}/check.sh /opt/master/
dos2unix /opt/master/check.sh
dos2unix /opt/master/conf/*

## 拷贝配置脚本
cp ${WORKSPACE}/config_codecpool.sh /usr/bin/
dos2unix /usr/bin/config_codecpool.sh

## 添加定时任务
sed -i '/check.sh/d' /etc/crontab
echo "*/1 * * * *     root    /opt/master/check.sh check >/dev/null 2>&1" >> /etc/crontab
service cron restart